﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using eAssessment.SPLinqPortal;
using Microsoft.SharePoint.Linq;
using System.Web;
using System.Data;
using eAssessment.lists;

namespace eAssessment.Classes.Lists
{
    class QuestionListManager
    {

        public int AddQuestionTemplate(string theme, string question, SPWeb listWeb)
        {
            if (question != null && question.Length > 0)
            {
                SPListItemCollection itemCollection = listWeb.Lists["eAssessment - Question-Templates"].Items;
                SPListItem newQuestion = itemCollection.Add();
                if (question.Length > 30)
                {
                    newQuestion["Titel"] = question.Substring(0, 30);
                }
                else
                {
                    newQuestion["Titel"] = question;
                }
                newQuestion["Question"] = question;
                newQuestion["Theme"] = theme;
                newQuestion.Update();
                return newQuestion.ID;
            }
            return -1;
        }

        public QuestionTemplate GetQuestionTemplate(int id, SPWeb listWeb)
        {
            var context = HttpContext.Current;
            HttpContext.Current = null;
            DataContext data = new DataContext(listWeb.Url);
            EntityList<QuestionTemplate> questions = data.GetList<QuestionTemplate>(ListAccessManager.QuestionTemplateListName);
            var query = from quest in questions
                        where quest.ID == id
                        select quest;
            HttpContext.Current = context;
            return query.First<QuestionTemplate>();
        }

        public DataTable GetQuestionTemplateDataTable(SPWeb listWeb)
        {
            SPList list = listWeb.Lists[ListAccessManager.QuestionTemplateListName];
            SPListItemCollection items = list.Items;
            return items.GetDataTable();
        }
    }
}
